package app.imttf.offline.backing.buatttf;

import java.util.Map;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import oracle.adf.view.rich.component.rich.RichPopup;
import oracle.adf.view.rich.component.rich.input.RichInputText;

import oracle.adf.view.rich.context.AdfFacesContext;

import org.apache.myfaces.trinidad.render.ExtendedRenderKitService;
import org.apache.myfaces.trinidad.util.Service;

public class MemoMdSubmitBackingBean {
    private RichInputText memoMdText;
    private RichPopup confirmMemoMdEmptyPopup;

    public MemoMdSubmitBackingBean() {
    }

    public void setMemoMdText(RichInputText memoMdText) {
        this.memoMdText = memoMdText;
    }

    public RichInputText getMemoMdText() {
        return memoMdText;
    }


    public static void showPopup(RichPopup popup, UIComponent component) {
        FacesContext context = FacesContext.getCurrentInstance();
        String alignId =
            (component == null) ? null : component.getClientId(context);
        StringBuilder script = new StringBuilder();
        script.append("var popup = AdfPage.PAGE.findComponent('").append(popup.getClientId(context)).append("'); ").append("if (!popup.isPopupVisible()) { ").append("var hints = {}; ");
        if (alignId != null) {
            script.append("hints[AdfRichPopup.HINT_ALIGN_ID] = '").append(alignId).append("'; ").append("hints[AdfRichPopup.HINT_ALIGN] = AdfRichPopup.ALIGN_AFTER_START; ");
        }
        script.append("popup.show(hints);}");
        ExtendedRenderKitService erks =
            Service.getService(context.getRenderKit(),
                               ExtendedRenderKitService.class);
        erks.addScript(context, script.toString());
    }

    public void setConfirmMemoMdEmptyPopup(RichPopup confirmMemoMdEmptyPopup) {
        this.confirmMemoMdEmptyPopup = confirmMemoMdEmptyPopup;
    }

    public RichPopup getConfirmMemoMdEmptyPopup() {
        return confirmMemoMdEmptyPopup;
    }

    public String submitTtf() {

        String goNext = null;

        AdfFacesContext adfFacesContext = AdfFacesContext.getCurrentInstance();
        Map<String, Object> pageFlowScope = adfFacesContext.getPageFlowScope();

        if (getMemoMdText().getValue() != null) {
            pageFlowScope.put("ttfValid", "V");
            goNext = "goFinalize";
        } else {
            showPopup(confirmMemoMdEmptyPopup, null);
        }

        return goNext;
    }
}
